package org.opengts.servers.template;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ClientDemo extends JFrame implements ActionListener {
	private JPanel panel = null;
	private JTextField messageBox = null;
	private JButton sendBtn = null;
	private JTextArea conversationBox = null;
	private String serverMessage = "", clientMessage = "";
	private String server = "localhost";
	private String[] data = null;
	private int dataLength = 10;
	
	private Socket socket = null;

	private BufferedReader in = null;
	private PrintWriter out = null;

	public ClientDemo() {
		setTitle("Client box");
		conversationBox = new JTextArea(5, 10);
		conversationBox.setEditable(false);
		add(new JScrollPane(conversationBox), BorderLayout.CENTER);
		panel = new JPanel();
		messageBox = new JTextField(20);
		sendBtn = new JButton("Send");
		sendBtn.addActionListener(this);
		panel.add(messageBox);
		panel.add(sendBtn);
		add(panel, BorderLayout.SOUTH);
		
		data = new String[dataLength];
		data[0] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[1] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[2] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[3] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[4] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[5] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[6] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[7] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[8] = "1111,2012/11/05,12:12:30,10.763623,106.676173,50,20";
		data[9] = "1111,2012/11/05,12:12:31,11.763623,106.676173,50,20";

		
	}

	public static void main(String[] args) {
		ClientDemo socketClient = new ClientDemo();
		socketClient.setSize(400, 400);
		socketClient.setVisible(true);
		socketClient.listenSocket();
	}

	public void listenSocket() {
		try {
			socket = new Socket(server, 31200);
			
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);

		} catch (UnknownHostException uhe) {
			System.out.println("Unknown host : " + server);
			System.exit(1);
		} catch (IOException ioe) {
			System.out.println("No I/O");
			System.exit(1);
		}

		while (true) {
			try {
				serverMessage = in.readLine();
				conversationBox.append("Server : " + serverMessage + "\n" );
			} catch (IOException e) {
				//System.out.println("Read failed");
				System.exit(-1);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {

		clientMessage = messageBox.getText();
		
		if (clientMessage.equals("")) {
			//messageBox.setText("");
			//conversationBox.append("Client : " + clientMessage + "\n");
			//out.println(clientMessage);
			
			for(String str : data)
			{
				out.println(str);
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
			if(clientMessage.equals("QUIT"))
			{
				try
				{
					in.close();
					out.close();
					socket.close();
					dispose();
				}
				catch(IOException ioe)
				{
					ioe.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
			}
		}

	}

}

